package nci60.data.maxquant.proteingroup
import nci60.data.maxquant.Project

/**
 * General information about the identified protein group (source: proteinGroup.txt)
 *
 * @autor <a href="mailto:Florian.J.Auer@googlemail.com">Florian J. Auer</a>
*/
class ProteinGroup {

    /**
     * Id attribute for the table. Is also used as primary key. <p />
     * This property is normaly generated and accessable for every domain class! 
     */
    Integer id
    
    /**
    * id used in MaxQuant output file
    */
    Integer idInFile
    
    /**
    * number of proteins contained within the group
    */
    Integer numberOfProteins
    
    /**
    * total number of peptide sequences associated with the protein group (i.e. for all the proteins in the group)
    */
    Integer numberOfPeptides
    
    /**
    * The total number of razor + unique peptides associated with the protein group (i.e. these peptides are shared with another protein group)
    */
    Integer numberOfRazorPeptides
    
    /**
    * The total number of unique peptides associated with the protein group (i.e. these peptides are not shared with another protein group)
    */
    Integer numberOfUniquePeptides
    
    /**
    * Percentage of the sequence that is covered by the identified peptides of the best protein sequence contained in the group
    */
    Double sequenceCoverage
    
    /**
    * Percentage of the sequence that is covered by the identified unique and razor peptides of the best protein sequence contained in the group
    */
    Double razorSequenceCoverage
    
    /**
    * Percentage of the sequence that is covered by the identified unique peptides of the best protein sequence contained in the group
    */
    Double uniqueSequenceCoverage
    
    /**
    * Molecular weight (kDa) of the leading protein sequence contained in the protein group
    */
    Double molWeight
    
    /**
    * Posterior Error Probability of the identification.
    * This value essentially operates as a p-value, where smaller is more significant
    */
    Double pep

    /**
    * Summed up eXtracted Ion Current (XIC) of all isotopic clusters associated with the identified AA sequence
    */
    Double intensity
    
    /**
    * intensity Based Absolute Quantification (iBAQ)
    */
    Double iBAQ

    /**
    * When true, this particular protein group was identified only by a modification site
    */
    boolean onlyIdentifiedBySite
    
    /**
    * When true, this particular protein group contains no protein, made up of at least 50% of the peptides of the leading protein, with a peptide derived from the reversed part of the decoy database.
    * These should be removed for further data analysis.
    * The 50% rule is in place to prevent spurious protein hits to erroneously flag the protein group as reverse
    */
    boolean reverse
    
    /**
    * When true, this particular protein group was found to be a commonly occurring contaminant.
    * These should be removed for further data analysis
    */
    boolean contaminant


    /**
    * reference to the analysis it belongs to <p />
    * @see nci60.data.maxquant.Project
    */
    Project currentProject

    
    /**
     * GRAILS <a href="http://grails.org/doc/latest/guide/GORM.html#gormAssociation" target="_blank">Associations in Gorm</a>
     * <p />
     * <style>.custom-table td{border-top:1pt solid grey;vertical-align:top;} .custom-table{border-collapse:collapse;width:100%;}</style>
     * <table class="custom-table">
     * <tr><td>
     *              experiments
     * </td><td>        protein group information specific for each cell line (experiment) <p />
     *                  <b>See Also:</b> {@link nci60.data.maxquant.proteingroup.ProteinGroupExperiment} <p />
     * </td></tr>
     * </table>
     */
    static hasMany = [experiments:ProteinGroupExperiment]

    /**
     * GRAILS <a href="http://grails.org/doc/latest/guide/single.html#constraints" target="_blank">Declaring Constraints</a>
     * <p />
     * Constraints in Grails are a way to declaratively specify validation rules for generating objects in GORM.
     */	
    static constraints = {
    }

    /**
     * GRAILS <a href="http://grails.org/doc/latest/guide/GORM.html#ormdsl" target="_blank">Custom ORM Mapping</a>
     * <p />
     * Grails domain classes can be mapped onto many legacy schemas with an Object Relational Mapping DSL (domain specific language). 
     */	
    static mapping = {
        
    }

    /**
     * Transforms the attribute values from the MaxQuant input file to values suitable for sql. e.g. "+", and "", are converted into "true", and "false" respectively. <p />
     * This mehtod is used while importing new data.
     * 
     * @param property  name of the property of this class (specified above)
     * @param input     attribute value in the MaxQuant output file
     * @return  replaced sql value of the input
     */		
    static String modifySQL(String property, String input) {
        String output
        if((property=='onlyIdentifiedBySite')||(property=='reverse')||(property=='contaminant')){
            if(input=='+'){
                output = 'true'
            }else{
                output = 'false'
            }
        }else if(property=='iBAQ'){
            if(input=='NaN'){
                output = '0'
            }else{
                output = input
            }
        }else{
            output = input
        }
        return output
    }

    /**
     * finds the protein group information for a given cell line name (experiment)
     * 
     * @param name  name of the cell line (experiment)
     * @return protein group experiment
     */	
    ProteinGroupExperiment giveExperimentByName(String name){
        def result = null
        experiments.each(){
            if(it.experiment.name==name){
                result = it
            }
        }
        return result
    }

    /**
     * finds the protein group information for a given cell line class (tissue)
     * 
     * @param cls  name of the cell line class (tissue of origin/experiment class)
     * @return List of protein group experiments
     */	
    List giveExperimentsByClass(String cls){
        def result = []
        experiments.each(){
            if(it.experiment.currentClass.name==cls){
                result.add(it)
            }
        }
        return result
    }

    /**
    * GRAILS default representation of the object as String
    * @return String representation of the object
    */	
    String toString(){
        return 'ProteinGroup '+id
    }
}
